<template>
  <van-cell-group inset>
    <van-cell>
      <van-field v-model="userId" label="userId" placeholder="输入用户ID" />
    </van-cell>
    <van-cell>
      <van-button @click="loginTest">
        测试登录
      </van-button>
    </van-cell>
    <van-cell>
      <van-button @click="loginCode">
        微信跳转登录
      </van-button>
    </van-cell>

    <van-cell>
      <van-button @click="getUserId">
        获取userId测试是否登录
      </van-button>
    </van-cell>

    <van-cell>
      <van-button @click="testParam">
        测试接口
      </van-button>
    </van-cell>
  </van-cell-group>
</template>

<script>
import {showToast} from "vant";
import 'vant/es/toast/style';
import authApi from "@/utils/api/authApi";
import wxApi from "@/utils/api/wxApi";
import userApi from "@/utils/api/userApi";
import axiosUtil from "@/utils/api/axiosUtil";

export default {
  name: "Test",
  data(){
    return {
      userId: '',
    }
  },
  methods: {
    async loginTest() {
      let userId = await authApi.loginTest(this.userId);
      showToast(`登陆成功，用户ID：${userId}`)
    },
    loginCode(){
      wxApi.openAuthCode(window.location.href)
    },
    async getUserId(){
      let userId = await userApi.getUserId()
      showToast(`当前用户ID：${userId}`)
    },
    async testParam(){
      // let getFormData = function (object) {
      //   const formData = new FormData()
      //   Object.keys(object).forEach(key => {
      //     const value = object[key]
      //     if (Array.isArray(value)) {
      //       value.forEach((subValue, i) =>
      //           formData.append(key + `[${i}]`, subValue instanceof Object ? getFormData(subValue):subValue)
      //       )
      //     } else {
      //       formData.append(key, object[key])
      //     }
      //   })
      //   return formData
      // }
      //
      // // let data = new FormData()
      // let qn = [
      //   {
      //     title: "title",
      //     type: 0,
      //     options: ["1","2"]
      //   }
      // ]
      // let data = {
      //   catIds: [1,2,3],
      //   qn
      // }
      // data.append("catIds", [1,2,3])
      // data.append("qn", JSON.stringify(qn))
      const createActParam = {
        catIds: [1, 2, 3],
        title: "Activity Title",
        info: "Activity Info",
        // ... other properties ...
        qn: [
          {
            title: "Question 1",
            type: 1,
            options: ["Option A", "Option B", "Option C"]
          },
          // ... other questions ...
        ]
      };

      const formData = new FormData();
      formData.append('act', new Blob([JSON.stringify(createActParam)], { type: 'application/json' }));
      await axiosUtil.post("/api/must/act/create", formData)
    }
  }
}
</script>

<style scoped>

</style>